Description
The following is the step to create a Client-Side validation using webwork. Note the validate attribute is set to true. Note also that not all themes support this feature (client-side validation)
Step 1
Create the jsp page. Note the <ww:head > tag being used, it will setup the css in this case (xhtml theme)
<html>
<head>
<title>Validation - Basic</title>
<ww:head />
</head>
<body>
<p>
The following form uses labelposition="left"
<ww:form id="f1" name="quizClient" namespace="/validation" method="post" validate="true">
<ww:textfield label="Name" name="name" labelposition="left" />
<ww:textfield label="Age" name="age" labelposition="left" />
<ww:textfield label="Favorite color" name="answer" labelposition="left" />
<ww:submit/>
</ww:form>
</p>
<br/>
<p>
The following form uses labelposition="top"
<ww:form id="f2" name="quizClient" namespace="/validation" method="post" validate="true">
<ww:textfield label="Name" name="name" labelposition="top" />
<ww:textfield label="Age" name="age" labelposition="top" />
<ww:textfield label="Favorite color" name="answer" labelposition="top" />
<ww:submit/>
</ww:form>
</p>
</body>
</html>
Step 2
Create the action class
public class QuizAction extends ActionSupport {
String name;
int age;
String answer;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
Step 3
Create the validation.xml to configure the validators to be used.
<!--
Add the following DOCTYPE declaration as first line of your XXX-validation.xml file:
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
-->
<validators>
<field name="name">
<field-validator type="requiredstring">
<message key="validation.name.required"/>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">13</param>
<param name="max">19</param>
<message key="validation.age.invalid" />
</field-validator>
</field>
</validators>
|